<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD><TITLE>lsort manual page - Tcl Built-In Commands</TITLE>
<link rel="stylesheet" href="../docs.css" type="text/css" media="all">
</HEAD>
<BODY><H2><a href="../contents.htm">Tcl8.6.11/Tk8.6.11 Documentation</a> <small>&gt;</small> <a href="contents.htm">Tcl Commands</a> <small>&gt;</small> lsort</H2>
<H3><A HREF="../UserCmd/contents.htm">Tcl/Tk Applications</A> | <A HREF="../TclCmd/contents.htm">Tcl Commands</A> | <A HREF="../TkCmd/contents.htm">Tk Commands</A> | <A HREF="../ItclCmd/contents.htm">[incr Tcl] Package Commands</A> | <A HREF="../SqliteCmd/contents.htm">SQLite3 Package Commands</A> | <A HREF="../TdbcCmd/contents.htm">TDBC Package Commands</A> | <A HREF="../TdbcmysqlCmd/contents.htm">tdbc::mysql Package Commands</A> | <A HREF="../TdbcodbcCmd/contents.htm">tdbc::odbc Package Commands</A> | <A HREF="../TdbcpostgresCmd/contents.htm">tdbc::postgres Package Commands</A> | <A HREF="../TdbcsqliteCmd/contents.htm">tdbc::sqlite3 Package Commands</A> | <A HREF="../ThreadCmd/contents.htm">Thread Package Commands</A> | <A HREF="../TclLib/contents.htm">Tcl C API</A> | <A HREF="../TkLib/contents.htm">Tk C API</A> | <A HREF="../ItclLib/contents.htm">[incr Tcl] Package C API</A> | <A HREF="../TdbcLib/contents.htm">TDBC Package C API</A></H3>
<DL>
<DD><A HREF="lsort.htm#M2" NAME="L1075">NAME</A>
<DL><DD>lsort &mdash; Sort the elements of a list</DD></DL>
<DD><A HREF="lsort.htm#M3" NAME="L1076">SYNOPSIS</A>
<DL>
</DL>
<DD><A HREF="lsort.htm#M4" NAME="L1077">DESCRIPTION</A>
<DL class="description">
<DD><A HREF="lsort.htm#M5" NAME="L1078"><B>-ascii</B></A>
<DD><A HREF="lsort.htm#M6" NAME="L1079"><B>-dictionary</B></A>
<DD><A HREF="lsort.htm#M7" NAME="L1080"><B>-integer</B></A>
<DD><A HREF="lsort.htm#M8" NAME="L1081"><B>-real</B></A>
<DD><A HREF="lsort.htm#M9" NAME="L1082"><B>-command </B><I>command</I></A>
<DD><A HREF="lsort.htm#M10" NAME="L1083"><B>-increasing</B></A>
<DD><A HREF="lsort.htm#M11" NAME="L1084"><B>-decreasing</B></A>
<DD><A HREF="lsort.htm#M12" NAME="L1085"><B>-indices</B></A>
<DD><A HREF="lsort.htm#M13" NAME="L1086"><B>-index </B><I>indexList</I></A>
<DD><A HREF="lsort.htm#M14" NAME="L1087"><B>-stride </B><I>strideLength</I></A>
<DD><A HREF="lsort.htm#M15" NAME="L1088"><B>-nocase</B></A>
<DD><A HREF="lsort.htm#M16" NAME="L1089"><B>-unique</B></A>
</DL>
<DD><A HREF="lsort.htm#M17" NAME="L1090">NOTES</A>
<DD><A HREF="lsort.htm#M18" NAME="L1091">EXAMPLES</A>
<DD><A HREF="lsort.htm#M19" NAME="L1092">SEE ALSO</A>
<DD><A HREF="lsort.htm#M20" NAME="L1093">KEYWORDS</A>
</DL>
<H3><A NAME="M2">NAME</A></H3>
lsort &mdash; Sort the elements of a list
<H3><A NAME="M3">SYNOPSIS</A></H3>
<B>lsort </B>?<I>options</I>? <I>list</I><BR>
<H3><A NAME="M4">DESCRIPTION</A></H3>
This command sorts the elements of <I>list</I>, returning a new
list in sorted order.  The implementation of the <B>lsort</B> command
uses the merge-sort algorithm which is a stable sort that has O(n log
n) performance characteristics.
<P>
By default ASCII sorting is used with the result returned in
increasing order.  However, any of the following options may be
specified before <I>list</I> to control the sorting process (unique
abbreviations are accepted):
<P>
<DL class="description">
<DT><A NAME="M5"><B>-ascii</B></A><DD>
Use string comparison with Unicode code-point collation order (the
name is for backward-compatibility reasons.)  This is the default.
<P><DT><A NAME="M6"><B>-dictionary</B></A><DD>
Use dictionary-style comparison.  This is the same as <B>-ascii</B>
except (a) case is ignored except as a tie-breaker and (b) if two
strings contain embedded numbers, the numbers compare as integers,
not characters.  For example, in <B>-dictionary</B> mode, <B>bigBoy</B>
sorts between <B>bigbang</B> and <B>bigboy</B>, and <B>x10y</B>
sorts between <B>x9y</B> and <B>x11y</B>. Overrides the <B>-nocase</B>
option.
<P><DT><A NAME="M7"><B>-integer</B></A><DD>
Convert list elements to integers and use integer comparison.
<P><DT><A NAME="M8"><B>-real</B></A><DD>
Convert list elements to floating-point values and use floating comparison.
<P><DT><A NAME="M9"><B>-command </B><I>command</I></A><DD>
Use <I>command</I> as a comparison command.
To compare two elements, evaluate a Tcl script consisting of
<I>command</I> with the two elements appended as additional
arguments.  The script should return an integer less than,
equal to, or greater than zero if the first element is to
be considered less than, equal to, or greater than the second,
respectively.
<P><DT><A NAME="M10"><B>-increasing</B></A><DD>
Sort the list in increasing order
(&ldquo;smallest&rdquo;items first).
This is the default.
<P><DT><A NAME="M11"><B>-decreasing</B></A><DD>
Sort the list in decreasing order
(&ldquo;largest&rdquo;items first).
<P><DT><A NAME="M12"><B>-indices</B></A><DD>
Return a list of indices into <I>list</I> in sorted order instead of
the values themselves.
<P><DT><A NAME="M13"><B>-index </B><I>indexList</I></A><DD>
If this option is specified, each of the elements of <I>list</I> must
itself be a proper Tcl sublist (unless <B>-stride</B> is used).
Instead of sorting based on whole sublists, <B>lsort</B> will extract
the <I>indexList</I>'th element from each sublist (as if the overall
element and the <I>indexList</I> were passed to <B><A HREF="../TclCmd/lindex.htm">lindex</A></B>) and sort
based on the given element.
For example,
<P>
<PRE><B>lsort</B> -integer -index 1 &#92;
      {{First 24} {Second 18} {Third 30}}</PRE>
<P>
returns <B>{Second 18} {First 24} {Third 30}</B>,
<P>
<PRE><B>lsort</B> -index end-1 &#92;
        {{a 1 e i} {b 2 3 f g} {c 4 5 6 d h}}</PRE>
<P>
returns <B>{c 4 5 6 d h} {a 1 e i} {b 2 3 f g}</B>,
and
<P>
<PRE><B>lsort</B> -index {0 1} {
    {{b i g} 12345}
    {{d e m o} 34512}
    {{c o d e} 54321}
}</PRE>
<P>returns <B>{{d e m o} 34512} {{b i g} 12345} {{c o d e} 54321}</B>
(because <B>e</B> sorts before <B>i</B> which sorts before <B>o</B>.)
This option is much more efficient than using <B>-command</B>
to achieve the same effect.
<P><DT><A NAME="M14"><B>-stride </B><I>strideLength</I></A><DD>
If this option is specified, the list is treated as consisting of
groups of <I>strideLength</I> elements and the groups are sorted by
either their first element or, if the <B>-index</B> option is used,
by the element within each group given by the first index passed to
<B>-index</B> (which is then ignored by <B>-index</B>). Elements
always remain in the same position within their group.
<P>
The list length must be an integer multiple of <I>strideLength</I>, which
in turn must be at least 2.
<P>
For example,
<P>
<PRE><B>lsort</B> -stride 2 {carrot 10 apple 50 banana 25}</PRE>
<P>
returns
&ldquo;apple 50 banana 25 carrot 10&rdquo;,
and
<P>
<PRE><B>lsort</B> -stride 2 -index 1 -integer {carrot 10 apple 50 banana 25}</PRE>
<P>returns
&ldquo;carrot 10 banana 25 apple 50&rdquo;.
<P><DT><A NAME="M15"><B>-nocase</B></A><DD>
Causes comparisons to be handled in a case-insensitive manner.  Has no
effect if combined with the <B>-dictionary</B>, <B>-integer</B>, or
<B>-real</B> options.
<P><DT><A NAME="M16"><B>-unique</B></A><DD>
If this option is specified, then only the last set of duplicate
elements found in the list will be retained.  Note that duplicates are
determined relative to the comparison used in the sort.  Thus if
<B>-index 0</B> is used, <B>{1 a}</B> and <B>{1 b}</B> would be
considered duplicates and only the second element, <B>{1 b}</B>, would
be retained.
<P></DL>
<H3><A NAME="M17">NOTES</A></H3>
The options to <B>lsort</B> only control what sort of comparison is
used, and do not necessarily constrain what the values themselves
actually are.  This distinction is only noticeable when the list to be
sorted has fewer than two elements.
<P>
The <B>lsort</B> command is reentrant, meaning it is safe to use as
part of the implementation of a command used in the <B>-command</B>
option.
<H3><A NAME="M18">EXAMPLES</A></H3>
Sorting a list using ASCII sorting:
<P>
<PRE><I>%</I> <B>lsort</B> {a10 B2 b1 a1 a2}
B2 a1 a10 a2 b1</PRE>
<P>
Sorting a list using Dictionary sorting:
<P>
<PRE><I>%</I> <B>lsort</B> -dictionary {a10 B2 b1 a1 a2}
a1 a2 a10 b1 B2</PRE>
<P>
Sorting lists of integers:
<P>
<PRE><I>%</I> <B>lsort</B> -integer {5 3 1 2 11 4}
1 2 3 4 5 11
<I>%</I> <B>lsort</B> -integer {1 2 0x5 7 0 4 -1}
-1 0 1 2 4 0x5 7</PRE>
<P>
Sorting lists of floating-point numbers:
<P>
<PRE><I>%</I> <B>lsort</B> -real {5 3 1 2 11 4}
1 2 3 4 5 11
<I>%</I> <B>lsort</B> -real {.5 0.07e1 0.4 6e-1}
0.4 .5 6e-1 0.07e1</PRE>
<P>
Sorting using indices:
<P>
<PRE><I>%</I> # Note the space character before the c
<I>%</I> <B>lsort</B> {{a 5} { c 3} {b 4} {e 1} {d 2}}
{ c 3} {a 5} {b 4} {d 2} {e 1}
<I>%</I> <B>lsort</B> -index 0 {{a 5} { c 3} {b 4} {e 1} {d 2}}
{a 5} {b 4} { c 3} {d 2} {e 1}
<I>%</I> <B>lsort</B> -index 1 {{a 5} { c 3} {b 4} {e 1} {d 2}}
{e 1} {d 2} { c 3} {b 4} {a 5}</PRE>
<P>
Sorting a dictionary:
<P>
<PRE><I>%</I> set d [dict create c d a b h i f g c e]
c e a b h i f g
<I>%</I> <B>lsort</B> -stride 2 $d
a b c e f g h i</PRE>
<P>
Sorting using striding and multiple indices:
<P>
<PRE><I>%</I> # Note the first index value is relative to the group
<I>%</I> <B>lsort</B> -stride 3 -index {0 1} &#92;
     {{Bob Smith} 25 Audi {Jane Doe} 40 Ford}
{{Jane Doe} 40 Ford {Bob Smith} 25 Audi}</PRE>
<P>
Stripping duplicate values using sorting:
<P>
<PRE><I>%</I> <B>lsort</B> -unique {a b c a b c a b c}
a b c</PRE>
<P>
More complex sorting using a comparison function:
<P>
<PRE><I>%</I> proc compare {a b} {
    set a0 [lindex $a 0]
    set b0 [lindex $b 0]
    if {$a0 &lt; $b0} {
        return -1
    } elseif {$a0 &gt; $b0} {
        return 1
    }
    return [string compare [lindex $a 1] [lindex $b 1]]
}
<I>%</I> <B>lsort</B> -command compare &#92;
        {{3 apple} {0x2 carrot} {1 dingo} {2 banana}}
{1 dingo} {2 banana} {0x2 carrot} {3 apple}</PRE>
<H3><A NAME="M19">SEE ALSO</A></H3>
<B><A HREF="../TclCmd/list.htm">list</A></B>, <B><A HREF="../TclCmd/lappend.htm">lappend</A></B>, <B><A HREF="../TclCmd/lindex.htm">lindex</A></B>, <B><A HREF="../TclCmd/linsert.htm">linsert</A></B>, <B><A HREF="../TclCmd/llength.htm">llength</A></B>, <B><A HREF="../TclCmd/lsearch.htm">lsearch</A></B>, <B><A HREF="../TclCmd/lset.htm">lset</A></B>, <B><A HREF="../TclCmd/lrange.htm">lrange</A></B>, <B><A HREF="../TclCmd/lreplace.htm">lreplace</A></B>
<H3><A NAME="M20">KEYWORDS</A></H3>
<A href="../Keywords/E.htm#element">element</A>, <A href="../Keywords/L.htm#list">list</A>, <A href="../Keywords/O.htm#order">order</A>, <A href="../Keywords/S.htm#sort">sort</A>
<div class="copy">Copyright &copy; 1993 The Regents of the University of California.
<BR>Copyright &copy; 1994-1996 Sun Microsystems, Inc.
<BR>Copyright &copy; 1999 Scriptics Corporation
<BR>Copyright &copy; 2001 Kevin B. Kenny &lt;kennykb(at)acm.org&gt;. All rights reserved.
</div>
</BODY></HTML>
